package open.witype.filetiper.custom;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import java.io.File;
import java.util.List;

/**
 * Created by Typer on 2015/6/1 0001.
 * PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
 */
public class DetermineIntent {

    public static Intent getImageFileIntent(String path){
        Intent intent = null;
        try {
            intent = new Intent(Intent.ACTION_VIEW);
            getCategory(intent);
            Uri uri = Uri.fromFile(new File(path));
            intent.setDataAndType(uri, "image/*");
        }catch (Exception e){
            e.printStackTrace();
        }
        return intent;
    }

    public static Intent getWordFileIntent(String path){
        Intent intent = null;
        try {
            intent = new Intent(Intent.ACTION_VIEW);
            getCategory(intent);
            Uri uri = Uri.fromFile(new File(path));
            intent.setDataAndType(uri,"application/msword");
        }catch (Exception e){
            e.printStackTrace();
        }
        return intent;
    }

    public static Intent getExcelFileIntent(String path){
        Intent intent = null;
        try {
            intent = new Intent(Intent.ACTION_VIEW);
            getCategory(intent);
            Uri uri = Uri.fromFile(new File(path));
            intent.setDataAndType(uri, "application/vnd-msexcel");
        }catch (Exception e){
            e.printStackTrace();
        }
        return intent;
    }

    public static Intent getPPTFileIntent(String path){
        Intent intent = null;
        try {
            intent = new Intent(Intent.ACTION_VIEW);
            getCategory(intent);
            Uri uri = Uri.fromFile(new File(path));
            intent.setDataAndType(uri, "application/vnd-powerpoint");
        }catch (Exception e){
            e.printStackTrace();
        }
        return intent;
    }

    public static boolean isIntentAvailable(Context context,Intent intent){
        final PackageManager manager = context.getPackageManager();
        List<ResolveInfo> resolveInfos = manager.queryIntentActivities(intent,PackageManager.GET_ACTIVITIES);
        return resolveInfos.size() > 0;
    }

    public static Intent getCategory(Intent intent){
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }
}
